package com.cloud.salon.client.business;

import com.cloud.salon.client.business.feign.MenberFeign;
import com.cloud.salon.client.business.service.PaymentService;
import com.cloud.salon.client.business.service.PreferentialService;
import com.cloud.salon.client.business.service.RecordItemService;
import com.cloud.salon.client.business.service.RecordService;
import com.cloud.salon.common.base.constant.PreferentialConstant;
import com.cloud.salon.common.base.constant.RecordStatusConstant;
import com.cloud.salon.common.base.entity.business.Payment;
import com.cloud.salon.common.base.entity.business.Preference;
import com.cloud.salon.common.base.entity.business.Record;
import com.cloud.salon.common.base.entity.menber.Account;
import com.cloud.salon.common.base.response.Res;
import com.cloud.salon.common.base.util.ValueUtil;
import com.cloud.salon.common.base.vo.business.PlaceOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/*
 * @Description 业务处理微服务
 * @Author wolf--LZH
 * @CreateDate 2019/4/26 0026 下午 9:56
 */
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EntityScan("com.cloud.salon.common.base.entity")
@ComponentScan(basePackages = {"com.cloud.salon"})

@RestController
public class SalonClientBusinessApplication {

    public static void main(String[] args) {
        SpringApplication.run(SalonClientBusinessApplication.class, args);
    }

    @Autowired
    MenberFeign menberFeign;
    @Autowired
    RecordService recordService;
    @Autowired
    PaymentService paymentService;
    @Autowired
    RecordItemService recordItemService;
    @Autowired
    PreferentialService preferentialService;

    @RequestMapping("/record/general")
    public String generalRecord() {

        String startStr = "2019-01-01";
        String endStr = "2019-05-19";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyMMdd");
        Date start = null, end = null;
        try {
            start = sdf.parse(startStr);
            end = sdf.parse(endStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Date current = new Date(start.getTime());
        // 添加一天
        Calendar c = Calendar.getInstance();
        c.setTime(current);
        c.add(Calendar.DAY_OF_MONTH, 1);
        Date next = c.getTime();

        Preference preference = new Preference();

        while (!current.after(end)) {

            int recordCount = (int)(Math.random() * 10);

            for (int i = 0; i < recordCount; i++) {
                Record record = new Record();
                // 随机产生会员id
                Integer accountId = (int)(Math.random() * 500);
                if (accountId < 94 || accountId > 193)
                    accountId = 0;
                preference.setAccountId(accountId);
                // 随机生成员工id
                Integer staffId = (int)(Math.random() * 5) + 1;
                // 生成订单日期
                Date date = new Date(current.getTime() + (long)(Math.random() * (next.getTime() - current.getTime())));

                // 生成订单关联的项目
                List<Integer> itemIdList = new ArrayList<>();
                List<Integer> preferenceIdList = new ArrayList<>();
                int itemCount = (int)(Math.random() * 5) + 1;
                for (int j = 0; j < itemCount; j++) {
                    Integer itemId = (int)(Math.random() * 5) + 1;

                    itemIdList.add(itemId);
                    preference.setItemId(itemId);

                    // 若会员结账,则进行优惠计算
                    if (accountId != 0) {
                        List<Preference> preferences = preferentialService.findByAccountAndItem(preference).getData();

                        Integer preferenceIndex = (int)(Math.random() * (preferences.size() + 1));
                        if (preferenceIndex >= preferences.size())
                            preferenceIdList.add(0);
                        else
                            preferenceIdList.add(preferences.get(preferenceIndex).getId());
                    }
                }
                if (accountId == 0) {
                    for (int j = 0; j < itemCount; j++)
                        preferenceIdList.add(0);
                }

                // 计算总价
                PlaceOrderVO placeOrderVO = new PlaceOrderVO();
                placeOrderVO.setAccountId(accountId);
                placeOrderVO.setStaffId(staffId);
                placeOrderVO.setItemIdList(itemIdList);
                placeOrderVO.setPreferenceIdList(preferenceIdList);
                Double total = ValueUtil.valueOfDouble(recordService.calcTotal(placeOrderVO).getData());

                record.setAccountId(accountId);
                record.setStaffId(staffId);
                record.setTotalPrice(total);
                record.setDate(date);
                record.setStatus(RecordStatusConstant.UNPAY);

                record = recordService.save(record).getData();

                // 设置记录项目关联
                recordItemService.setRecordItems(record.getId(), itemIdList, preferenceIdList);

                // 支付
                Payment payment = new Payment();
                payment.setRecordId(record.getId());
                payment.setPayAmount(total);
                payment.setChange(0.0);

                if (accountId != 0)
                    payment.setType((int)(Math.random() * 2) + 1);
                else
                    payment.setType(1);
                payment.setPayDate(date);

                paymentService.save(payment);
            }

            current = next;
            c.add(Calendar.DAY_OF_MONTH, 1);
            next = c.getTime();

        }

        return "done";
    }

    @RequestMapping("/preference/general")
    public String generalPreference() {

        List<Account> accounts = menberFeign.findAllAccount().getData();

        for (Account account: accounts) {

            int preferenceCount = (int)(Math.random() * 11);

            for (int i = 0; i < preferenceCount; i++) {
                Preference preference = new Preference();

                preference.setAccountId(account.getId());
                preference.setItemId((int)(Math.random() * 5) + 1);
                preference.setTimes((int)(Math.random() * 50));
                preference.setMinConsume(0.0);

                Integer type = (int)(Math.random() * 2);
                if (type == 0) {
                    preference.setDiscount((int)(Math.random() * 100) / 100.0);
                }
                else if(type == 1) {
                    preference.setVoucher(Double.valueOf((int)(Math.random() * 200)));
                }

                preference.setType(type);

                preferentialService.save(preference);
            }
        }

        return "done";
    }

}
